package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userAddressController")
@Api(tags = "地址相关接口")
@Slf4j
@RequestMapping("/user/addressBook")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @ApiOperation(value = "新增地址信息")
    @PostMapping()
    public Result add(@RequestBody AddressBook addressBook) {
        log.info("addressBook: {}", addressBook);
        addressService.add(addressBook);
        return Result.success();
    }

    /**
     * 查询用户所有地址信息
     * @return
     */
    @ApiOperation(value = "查询用户所有地址信息")
    @GetMapping("/list")
    public Result<List<AddressBook>> getlist() {
        List<AddressBook> addressBookList = addressService.getAddressList();
        return Result.success(addressBookList);
    }

    /**
     * 根据id查询用户地址
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询用户地址")
    @GetMapping("/{id}")
    public Result<AddressBook> get(@PathVariable Long id) {
        log.info("id: {}", id);
        AddressBook addressBook = addressService.getAddressById(id);
        return Result.success(addressBook);
    }

    /**
     * 查询用户默认地址
     * @return
     */
    @ApiOperation(value = "查询用户默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getDefaultAddress() {
        AddressBook addressBook = addressService.getDefaultAddress();
        return Result.success(addressBook);
    }

    /**
     * 修改用户地址信息
     * @param addressBook
     * @return
     */
    @ApiOperation(value = "修改用户地址信息")
    @PutMapping()
    public Result update(@RequestBody AddressBook addressBook) {
        log.info("addressBook: {}", addressBook);
        addressService.update(addressBook);
        return Result.success();
    }

    /**
     * 修改默认地址
     * @param addressBook
     * @return
     */
    @ApiOperation(value = "修改默认地址")
    @PutMapping("/default")
    public Result updateDefault(@RequestBody AddressBook addressBook) {
        log.info("addressBook: {}", addressBook);
        addressService.updateDefault(addressBook);
        return Result.success();
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除地址")
    @DeleteMapping()
    public Result delete(Long id) {
        log.info("id: {}", id);
        addressService.delete(id);
        return Result.success();
    }

}
